'''
插值
'''
import pandas as pd

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('expand_frame_repr',False)

def interp(onetrajdata,threshold):
    BEGINTIME = int(onetrajdata.iloc[0]["Unixtime"])
    ENDINTIME = int(onetrajdata.iloc[-1]["Unixtime"])
    TRAJ_SEGMENT = int(onetrajdata.iloc[0]["traj_segment"])
    MMSI = int(onetrajdata.iloc[0]["mmsi"])
    timelist = [BEGINTIME]
    temp = BEGINTIME
    while temp <= ENDINTIME:
        temp = temp + threshold
        timelist.append(temp)
    newdata = pd.DataFrame(
        {'Unixtime': timelist, 'mmsi': MMSI, 'longitude': None, 'latitude': None, 'cog': None, 'sog': None,
         'shiptype': 0, 'shiplength': 0, 'shipwidth': 0, 'traj_segment': TRAJ_SEGMENT})
    tmp = pd.concat([onetrajdata, newdata])
    finaldata = tmp.set_index('Unixtime').interpolate(method='index').sort_values(by=['Unixtime']).reset_index()
    finaldata = finaldata.drop_duplicates(subset=['mmsi', 'Unixtime']).reset_index(drop=True)
    return finaldata

def getinterpdata(data,threshold):
    newdata = pd.DataFrame(columns=(
    'Unixtime', 'mmsi', 'longitude', 'latitude', 'cog', 'sog', 'shiptype', 'shiplength', 'shipwidth', 'traj_segment'))
    MMSIDATA = data['mmsi']
    SHIPSMMSI = list(set(MMSIDATA))
    shipcount = len(SHIPSMMSI)
    for i in range(shipcount):
        mmsi = SHIPSMMSI[i]
        ONESHIP = data.iloc[:, :][data[data.T.index[1]] == mmsi]  # 找出这艘船
        TRAJ_SEGMENT = list(set(ONESHIP["traj_segment"]))  # 这艘船轨迹段的set集合
        for j in (TRAJ_SEGMENT):
            ONETRAJ = ONESHIP.iloc[:, :][ONESHIP[ONESHIP.T.index[9]] == j]
            finaldata = interp(ONETRAJ, threshold)
            tmp = pd.concat([newdata, finaldata])
            newdata = tmp
    newdata = newdata.sort_values(by=['mmsi', 'Unixtime']).reset_index(drop=True)
    return newdata





